﻿# Returns a service application given the name
Function Get-SPApplicationByName 
{
	Param($name)

	Get-SPServiceApplication | ? { $_.Name -eq $name }
}

#Return a service application proxy given the name

Function Get-SPApplicationProxyByName
{
	Param($name)
	$name = "*" + $name + "*"
	Get-SPServiceApplicationProxy | ? {$_.Name -like $name}
}


Function Get-SPManagedAccountByName
{
	Param($accountName)
	
	if ($accountName -eq "")
	{
		$accountName = [System.Environment]::UserDomainName + "\" + [System.Environment]::UserName
		write-host ("The current account is " + $accountName)
	}

	Get-SPManagedAccount | ? {$_.Name -like $accountName}
}

Function New-WebApplication
{
	Param($machineName, [int32]$port, $appPoolAccount)

	if ($machineName -eq "")
	{
		$machineName = [System.Environment]::MachineName
	}

	
	if ($appPoolAccount -eq "")
	{
		$appPoolAccount = [System.Environment]::UserDomainName + "\" + [System.Environment]::UserName
	}

	$webName = "Sharepoint" + $port
	$webURL = "http://" + $machineName + ":" + $port
	
	$appPool = $webName + "appPool"
	

	write-host ([System.String]::Format("Creating a web application: {0}, app pool account {1}", $webURL, $appPoolAccount))
	
	New-SPWebApplication -Name $webName -port $port -AuthenticationMethod "NTLM" -ApplicationPoolAccount $appPoolAccount -ApplicationPool $appPool

	if (!$?)
	{
		write-host("an error occurred")
		exit
	} 
}

Function Start-SPServiceByTypeNameOnserver
{
	Param($serviceTypeName, $serverName)

	if ($serverName -eq "")
	{
		$serverName = [System.Environment]::MachineName
	}
	$serverName = "*" + $serverName

	$serviceTypeName = "*" + $serviceTypeName

	$serviceInstance = Get-SPServiceInstance | ? {$_.TypeName -like $serviceTypeName -And $_.Server -like $serverName}
	
	if ($serviceInstance -eq $null)
	{
		write-host ("Cannot find the service instance with type name " + $serviceTypeName + " on " + $serverName)
		exit
	}
	
	Start-SPServiceInstance -identity $serviceInstance

	if (!$?)
	{
		write-host("an error occurred when starting the service")
		exit
	}
}

####################
# Create new web application and 
# return the web application
####################
Function New-WebApplication
{
	PARAM($machineName, [int32]$port, $appPoolAccount)

	if ($machineName -eq "")
	{
		$machineName = [System.Environment]::MachineName
	}

	
	if ($appPoolAccount -eq "")
	{
		$appPoolAccount = [System.Environment]::UserDomainName + "\" + [System.Environment]::UserName
	}

	$webName = "Sharepoint" + $port
	$webURL = "http://" + $machineName + ":" + $port
	
	$appPool = $webName + "AppPool"
	
	write-host ([System.String]::Format("Creating a web application: {0}, app pool account {1}", $webURL, $appPoolAccount))
	
	$app = New-SPWebApplication -Name $webName -port $port -AuthenticationMethod "NTLM" -ApplicationPoolAccount $appPoolAccount -ApplicationPool $appPool

	if (!$?)
	{
		write-host("an error occurred when creating a web application")
		exit
	} 

	return $app
}

##########
# Create a Site Collection (Templates include SPSMSITEHOST#0, SPSPORTAL#0, etc) and
# return the site collection
##########
Function New-SiteCollection
{
	PARAM($url, $ownerEmail, $ownerAlias, $siteTemplate)

	if ($ownerEmail -eq "")
	{
		$ownerEmail = "admin@microsoft.com"
	}

	if ($ownerAlias -eq "")
	{
		$ownerAlias = [System.Environment]::UserDomainName + "\" + [System.Environment]::UserName
	}

	if ($siteTemplate -eq "")
	{
		$siteTemplate = "SPSPORTAL#0" 
	}

	$site = New-SPSite -Url $url -OwnerEmail $ownerEmail -OwnerAlias $ownerAlias 
	if (!$?)
	{
		write-host ("An error has occurred when creating a site collection.")
		Exit
	}

	$site.RootWeb.ApplyWebTemplate($siteTemplate)
	if (!$?)
	{
		write-host ("An error has occurred when applying site template")
		Exit
	}

	return $site
}

##########
# get the SPIisWebServiceApplicationPool
# 	create one if it doesn't exist
##########
Function Get-WebServiceApplicationPool
{
	PARAM ($name, $credential)
	
	if ($credential -eq "")
	{
		$credential = [System.Environment]::UserDomainName + "\" + [System.Environment]::UserName
	}
	
	$appPool = Get-SPIisWebServiceApplicationPool | ? {$_.Name -eq $name}
	if ($appPool -eq $null)
	{
		Write-Host ([System.String]::Format("Creating the web service application pool: {0}", $name))
		$appPool = New-SPIisWebServiceApplicationPool -Name $name -Account $credential
		
		if (!$?)
		{
			Write-Host ("An error has occurred when creating web service application pool")
			Exit
		}
	}
	return $appPool
}



Function Attach-ContentDB
{
	PARAM ( $URL, $DBName, $DBServer)

	#echo "$URL, $DBServer, $DBName"
	if( $DBName -eq "" )
	{
		write-error "Attach-ContentDb: Database name not specified."
		return $false;
	}

	if( $URL -eq "" )
	{
		write-error "Attach-ContentDb: URL not specified."
		return $false;
	}

	echo ""
	echo "Attaching content db $DBName..."

	$StartTime = Get-Date
	if( $DBServer -eq "" )
	{
		 echo "stsadm -o addcontentdb -url $URL -databasename $DBName"
	     stsadm -o addcontentdb -url $URL -databasename $DBName 
	}
	else
	{
		 echo "stsadm -o addcontentdb -url $URL -databasename $DBName -databaseserver $DBServer"
	     stsadm -o addcontentdb -url $URL -databasename $DBName -databaseserver $DBServer
 	}

	# If failed, upgrade it again
	#echo "Upgrade content db again..."
	$success = $true;
	if( $lastexitcode -ne 0 )
	{
	     $contentdb = get-SPcontentdatabase | ? { $_.name -eq $DBName }
	     if ( $contentdb -ne $null )
	     {
		  	upgrade-spcontentdatabase -Identity $contentdb.Id.ToString();
			if( ! $? )
			{
				$success = $false;
			}
 	     }
 	}
	
	$EndTime = Get-Date
	
	$UpgradeTime = New-TimeSpan -start $StartTime -end $EndTime
	echo "Finished upgrade!"
	echo "Time Used: $UpgradeTime"	
	echo ""

	#return $success;
}

Function IsElevated
{
	$user = "$env:USERDOMAIN\$env:USERNAME";
	
	$isAdmin =  net localgroup administrators | ? { $_ -contains $user }
	
	if ( $isAdmin -ne $null )
	{
		return $true;
	}
	else
	{
		return $false;
	}
}
